package com.my.Config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@ComponentScan(value = "com.my",includeFilters = {
  @ComponentScan.Filter(classes = Controller.class)
},useDefaultFilters = false)
@EnableWebMvc
public class ServletConfig implements WebMvcConfigurer {



  @Autowired
  WebApplicationContext webApplicationContext;

//
//  <bean id="templateResolver"
//  class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
//    <property name="prefix" value="/WEB-INF/templates/" />
//    <property name="suffix" value=".html" />
//    <property name="templateMode" value="HTML5" />
//  </bean>
//
//  <bean id="templateEngine"
//  class="org.thymeleaf.spring4.SpringTemplateEngine">
//    <property name="templateResolver" ref="templateResolver" />
//  </bean>
//
//  <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
//    <property name="templateEngine" ref="templateEngine" />
//  </bean>

  @Bean // 配置生成模板解析器
  public ITemplateResolver templateResolver() {
 // WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
    // ServletContextTemplateResolver需要一个ServletContext作为构造参数，可通过WebApplicationContext 的方法获得
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(
      webApplicationContext.getServletContext());
    templateResolver.setPrefix("/WEB-INF/thymeleaf/");
    templateResolver.setSuffix(".html");
    // templateResolver.setCharacterEncoding("UTF-8");
    // 设置模板模式,也可用字符串"HTML"代替,此处不建议使用HTML5,原因看下图源码
    templateResolver.setTemplateMode(TemplateMode.HTML);
    return templateResolver;
  }

  @Bean // 生成模板引擎并为模板引擎注入模板解析器
  public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    return templateEngine;
  }

//  @Bean // 生成视图解析器并未解析器注入模板引擎
//  public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
//    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
//    viewResolver.setContentType("text/html; charset=utf-8");
//    viewResolver.setTemplateEngine(templateEngine);
//    return viewResolver;
//  }


  @Override
  public void configureViewResolvers(ViewResolverRegistry registry) {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setContentType("text/html; charset=utf-8");
    viewResolver.setTemplateEngine(templateEngine(templateResolver()));

    registry.viewResolver(viewResolver);
  }





}
